package uk.ac.bbk.dcs.muc;

/**
 * Copyright [2011] [Sean Howes and Aruna Gunasekera]
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import android.database.sqlite.SQLiteDatabase;
import android.media.MediaRecorder;
import android.util.Log;

public class SoundRecorderService {

	private MediaRecorder soundRecorder;

	private SQLiteDatabase database;

	public SoundRecorderService(SQLiteDatabase database) {
		this.database = database;
	}

	public void recordMaxAmplitude(Long now) {
		EventSqlLiteHelper.writeRecordToDb("Sound Levels", now, String.valueOf(soundRecorder.getMaxAmplitude()), database);
	}

	public void beginRecording() {
		try {
			if (soundRecorder == null) {
				soundRecorder = new MediaRecorder();
			}
			soundRecorder.reset();
			soundRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			soundRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			soundRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			soundRecorder.setOutputFile("/dev/null");
			soundRecorder.prepare();
			soundRecorder.start();
			
		} catch (Exception e) {
			Log.e("Recording Error", "Could not start recording", e);
		}
	}

	public void stop() {
		soundRecorder.stop();	
	}
}
